<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="lib/layui/css/layui.css">
  <script src="lib/layui/layui.js"></script>
  <link rel="stylesheet" href="/css/user-login.css">
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  <title>管理员登录</title>
</head>

<body>
  <div class="layui-fluid">
    <div class="layui-row">
      <div class="logo">
        <img src="/images/logo.png" alt="传智书城Logo">
      </div>
    </div>
    <div class="layui-row">
      <div class="title">
        <h1>传智书城后台管理系统
          <h2>
            管理员登录
          </h2>
        </h1>
      </div>
    </div>
    <div class="layui-row">
      <div class="login-form">
        <form class="layui-form" id="login-form">
          <div class="layui-row">
            <label class="layui-col-xs3">账号</label>
            <div class="layui-input-inline layui-col-xs9">
              <input type="text" name="userName" required lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-row">
            <label class="layui-col-xs3">密码</label>
            <div class="layui-input-inline layui-col-xs9">
              <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-row">
            <h6 id="info"></h6>
          </div>
          <div class="layui-row">
            <div class="layui-col-xs3"></div>
            <div class="layui-col-xs9" style="position: relative;">
              <button class="layui-btn layui-btn-normal layui-col-xs5" lay-submit lay-filter="loginForm">登录</button>
              <button type="reset" class="layui-btn layui-btn-primary layui-col-xs5" style="position: absolute;right: 10px;">重置</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>

</body>

</html>
<script src="js/utils.js"></script>
<script>
  layui.use(['form', 'laydate'], function() {
    var form = layui.form;
    let laydate = layui.laydate;

    //监听提交
    form.on('submit(loginForm)', function(data) {
      $.ajax({
        url: baseURL + "/BookUserController/UserLogin",
        data: data.field,
        method: "post",
        success(res) {
          console.log(res);
          $("#info").text("登录成功！").css("color", "lightGreen");
          setCookie("USER", JSON.stringify(res.data.user));
          setCookie("token", JSON.stringify(res.data.token));
          window.location.href = "./index.html";
        },
        error(err) {
          $("#info").text("登录失败！").css("color", "red");
        }
      });
      return false;
    });

  });
</script>